package com.roadmap.struts.action.admin.life;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.BaseConst;
import com.roadmap.common.RdcConst;
import com.roadmap.entities.Article;
import com.roadmap.struts.action.ActionConst;
import com.roadmap.struts.action.RdcDispatchAction;

public class BlogAction extends RdcDispatchAction {

	@Override
	protected ActionForward doAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ownerId = request
				.getParameter(ActionConst.Aspirant_Id_In_Request);
		if (ownerId == null || ownerId.trim().length() == 0) {
			return handleInvalidUser(request, "/blog.do?act=all");
		}

		String currentUser = getUIDFromSession(request);
		int currentPage = getCurrentPageNum(request);
		IManager blogMgr = ManagerFactory.getInstance(RdcConst.BLOG,
				currentUser, ownerId);
		blogMgr.setPage(currentPage);
		List<Article> articles = blogMgr.getEntitiesOfOwner();
		if (articles != null && !articles.isEmpty()) {
			request.setAttribute("articles", articles);
			createPagingInfoInRequest(request, blogMgr);
		}
		return mapping.findForward("getarticles");
	}

	@Override
	protected ActionForward doView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ownerId = request
				.getParameter(ActionConst.Aspirant_Id_In_Request);
		if (ownerId == null || ownerId.trim().length() == 0) {
			return handleInvalidUser(request, "/blog.do?act=all");
		}

		String artId = request.getParameter(ActionConst.Item_Id_In_Request);
		if (artId == null) {
			return handleInvalidUser(request, "/blog.do?act=all");
		}

		String currentUser = getUIDFromSession(request);
		if (artId != null) {
			IManager blogMgr = ManagerFactory.getInstance(RdcConst.BLOG,
					currentUser, ownerId);
			Article art = (Article) blogMgr.get(artId);
			if (art != null) {
				request.setAttribute("article", art);

				IManager cmMgr = ManagerFactory.getInstance(RdcConst.COMMENT,
						currentUser, ownerId, RdcConst.BLOG);
				List comments = cmMgr.getEntitiesOfParent(artId);
				if (comments != null && !comments.isEmpty()) {
					request.setAttribute("comments", comments);
					request.setAttribute("hasMoreComments",
							cmMgr.getMaxResultCount() > BaseConst.MAX_RESULTS_IN_QUERY);
				}
			}
		}

		if (!currentUser.equals(ownerId)) {
			request.removeAttribute(ActionConst.Visitor_Is_Owner);
		}

		return mapping.findForward("getarticle");
	}

	@Override
	protected ActionForward doCreate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("getarticle");
	}

	@Override
	public boolean isFormSubmitted(HttpServletRequest request) throws Exception {
		return false;
	}
}
